import pymysql
from pymysql.connections import Connection
import sqlite3
from warnings import filterwarnings

from Common.ReadYaml import ConfigYaml
from Common.Route import Any_Path

filterwarnings('ignore', category=pymysql.Warning)


class Sql:
    def __init__(self):
        '''

        :param sql:  sql语句
        :param db:  数据库名称
        '''


        self.mysql = ConfigYaml('mysql').base_config
        self.sqlite_path = Any_Path('Config', self.db)
        self.sqlite = ConfigYaml('sqlite').base_config
        self.host = "10.1.3.225"
        self.port = int(ConfigYaml('port').base_config)
        self.pwd = "erp-pdm@123"
        self.sqlname = sqlname


        if self.sqlite:
            try:
                self.conn = sqlite3.connect(self.sqlite_path)
            except:
                raise ConnectionError("sqlite connect error,please check configuration ....")
        else:
            try:
                self.conn = Connection(host=self.host, user=self.user, password=self.pwd, database=self.sqlname,
                                       port=self.port, charset='utf8')
            except:
                raise ConnectionError("mysql connect error,please check configuration ....")

    def execute_sql(self):
        '''
        执行sql语句
        :return:
        '''
        if self.switch:
            cursor = self.conn.cursor(pymysql.cursors.DictCursor)
        else:
            cursor = self.conn.cursor()
        cursor.execute(self.sql)
        values = cursor.fetchall()
        self.conn.commit()
        cursor.close()
        self.conn.close()
        return values



if __name__=="__main__":
    create_response = "CREATE TABLE IF NOT EXISTS response(id INTEGER primary key NOT NULL AUTO_INCREMENT,funname varchar (100),describes varchar(1000),priority varchar(50),address varchar(1000),result varchar(30), writer varchar(30),usetime float ,summary text(20000000));"
    create_result = "CREATE TABLE IF NOT EXISTS result(id INTEGER primary key NOT NULL AUTO_INCREMENT,funname varchar (100),totalnumber int default 0,passnumber int default 0, errornumber int default 0,failnumber int default 0,passrate float default 0,errorrate float default 0,failrate float default 0,modular varchar (30));"
    response = Sql(sql=create_result,switch=False).execute_sql()








